package com.ruoyi.orderParent.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.orderParent.domain.OrderDetail;
import com.ruoyi.orderParent.domain.SysOrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.orderParent.domain.SysOrder;
import com.ruoyi.orderParent.mapper.SysOrderParentMapper;
import com.ruoyi.orderParent.domain.SysOrderParent;
import com.ruoyi.orderParent.service.ISysOrderParentService;

/**
 * 批量订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-24
 */
@Service
public class SysOrderParentServiceImpl implements ISysOrderParentService 
{
    @Autowired
    private SysOrderParentMapper sysOrderParentMapper;

    /**
     * 查询批量订单
     * 
     * @param id 批量订单主键
     * @return 批量订单
     */
    @Override
    public SysOrderParent selectSysOrderParentById(Long id)
    {
        return sysOrderParentMapper.selectSysOrderParentById(id);
    }

    /**
     * 查询批量订单列表
     * 
     * @param sysOrderParent 批量订单
     * @return 批量订单
     */
    @Override
    public List<SysOrderParent> selectSysOrderParentList(SysOrderParent sysOrderParent)
    {
        return sysOrderParentMapper.selectSysOrderParentList(sysOrderParent);
    }

    /**
     * 新增批量订单
     * 
     * @param sysOrderParent 批量订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysOrderParent(SysOrderParent sysOrderParent)
    {
        sysOrderParent.setCreateTime(DateUtils.getNowDate());
        int rows = sysOrderParentMapper.insertSysOrderParent(sysOrderParent);
        insertSysOrder(sysOrderParent);
        return rows;
    }

    /**
     * 修改批量订单
     * 
     * @param sysOrderParent 批量订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysOrderParent(SysOrderParent sysOrderParent)
    {
        sysOrderParent.setUpdateTime(DateUtils.getNowDate());
        sysOrderParentMapper.deleteSysOrderByParentId(sysOrderParent.getId());
        insertSysOrder(sysOrderParent);
        return sysOrderParentMapper.updateSysOrderParent(sysOrderParent);
    }

    /**
     * 批量删除批量订单
     * 
     * @param ids 需要删除的批量订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysOrderParentByIds(Long[] ids)
    {
        sysOrderParentMapper.deleteSysOrderByParentIds(ids);
        return sysOrderParentMapper.deleteSysOrderParentByIds(ids);
    }

    /**
     * 删除批量订单信息
     * 
     * @param id 批量订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysOrderParentById(Long id)
    {
        sysOrderParentMapper.deleteSysOrderByParentId(id);
        return sysOrderParentMapper.deleteSysOrderParentById(id);
    }

    @Override
    public List<SysOrderDetail> getSysOrderParentById(Long id) {
        return sysOrderParentMapper.getSysOrderParentById(id);
    }

    @Override
    public List<SysOrderDetail> getSysOrderDetail(OrderDetail orderDetail,Long id) {
        return sysOrderParentMapper.getSysOrderDetail(orderDetail,id);
    }

    @Override
    public List<SysOrderParent> getSysOrderParentList(SysOrderParent sysOrderParent, Date beginCreateTime, Date endCreateTime) {
        return sysOrderParentMapper.getSysOrderParentList(sysOrderParent,beginCreateTime,endCreateTime);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param sysOrderParent 批量订单对象
     */
    public void insertSysOrder(SysOrderParent sysOrderParent)
    {
        List<SysOrder> sysOrderList = sysOrderParent.getSysOrderList();
        Long id = sysOrderParent.getId();
        if (StringUtils.isNotNull(sysOrderList))
        {
            List<SysOrder> list = new ArrayList<SysOrder>();
            for (SysOrder sysOrder : sysOrderList)
            {
                sysOrder.setParentId(id);
                list.add(sysOrder);
            }
            if (list.size() > 0)
            {
                sysOrderParentMapper.batchSysOrder(list);
            }
        }
    }
}
